package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Log;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.LogService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public void save(Log log) {
        logMapper.insert(log);
    }

    @Override
    public PageBeanVo findAll(Integer pageNumber, Integer pageSize, String startDate, String endDate) {
        //拼接分页参数
        IPage page = new Page(pageNumber, pageSize);
        //拼接分页条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ge("log_time", startDate);
        queryWrapper.le("log_time", endDate);
        queryWrapper.orderByDesc("log_time");
        page = logMapper.selectPage(page, queryWrapper);

        return new PageBeanVo(pageNumber, pageSize, page.getTotal(), page.getRecords());
    }

    @Override
    public PageBeanVo findAll(Integer pageNumber, Integer pageSize) {
        //拼接分页参数
        IPage page = new Page(pageNumber, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("log_time");
        page = logMapper.selectPage(page, queryWrapper );
        return new PageBeanVo(pageNumber, pageSize, page.getTotal(), page.getRecords());

    }

    @Override
    public PageBeanVo findByNickName(Integer pageNumber, Integer pageSize, String startDate, String endDate, Long userId) {

        //拼接分页参数
        IPage page = new Page(pageNumber, pageSize);
        //拼接分页条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ge("log_time", startDate);
        queryWrapper.le("log_time", endDate);
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("log_time");
        page = logMapper.selectPage(page, queryWrapper );

        return new PageBeanVo(pageNumber, pageSize, page.getTotal(), page.getRecords());

    }


}
